<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="pmsSkuIn" :rules="rules" ref="PmsSkuInFrom" label-width="150px">
      <!-- 禁止填写 -->
      <el-form-item label="进货单编号" prop="innum">
        <el-input v-model="pmsSkuIn.innum" style="width: 370px;" />
      </el-form-item>

      <el-form-item label="操作人" prop="operName">
        <el-input v-model="pmsSkuIn.operName" style="width: 370px;" />
      </el-form-item>

      <el-form-item label="供应商" prop="operTime">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="pmsSkuIn.operTime"
          style="width: 370px;"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="pmsSkuIn.price" style="width: 370px;" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('PmsSkuInFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('PmsSkuInFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import {
  createPmsSkuIn,
  getPmsSkuIn,
  updatePmsSkuIn
} from "@/api/prm/pmsSkuIn";
import { getProduct } from "@/api/product";
import { fetchListWithChildren } from "@/api/productCate";
import SingleUpload from "@/components/Upload/singleUpload";

const defaultPmsSkuIn = {
  name: ""
};
export default {
  name: "PmsSkuInDetail",
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectProductCateValue: [],
      productCateOptions: [],
      productParam: {},
      pmsSkuIn: Object.assign({}, defaultPmsSkuIn),
      rules: {
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ],
        logo: [{ required: true, message: "请输入品牌logo", trigger: "blur" }],
        sort: [{ type: "number", message: "排序必须为数字" }]
      }
    };
  },
  watch: {
    selectProductCateValue: function(newValue) {
      console.log(newValue);
      if (newValue != null) {
        this.value.productCategoryId =
          newValue[newValue.length - 1 > 0 ? newValue.length - 1 : 0];
        this.value.productCategoryName = this.getCateNameById(
          this.value.productCategoryId
        );
      } else {
        this.value.productCategoryId = null;
        this.value.productCategoryName = null;
      }
    }
  },
  created() {
    if (this.isEdit) {
      getPmsSkuIn(this.$route.query.id).then(response => {
        this.pmsSkuIn = response.data;
      });
    } else {
      this.pmsSkuIn = Object.assign({}, defaultPmsSkuIn);
      this.pmsSkuIn.productId = this.$route.query.id;
      // this.getProductInfo();
    }
    // this.getProductCateList();
  },
  methods: {
    // getProductCateList() {
    //   fetchListWithChildren().then(response => {
    //     let list = response.data;
    //     this.productCateOptions = [];
    //     for (let i = 0; i < list.length; i++) {
    //       let children = [];
    //       if (list[i].children != null && list[i].children.length > 0) {
    //         for (let j = 0; j < list[i].children.length; j++) {
    //           children.push({
    //             label: list[i].children[j].name,
    //             value: list[i].children[j].id
    //           });
    //         }
    //       }
    //       this.productCateOptions.push({
    //         label: list[i].name,
    //         value: list[i].id,
    //         children: children
    //       });
    //     }
    //   });
    // },
    // getProductInfo() {
    //   let _this = this;
    //   getProduct(this.$route.query.id).then(response => {
    //     _this.productParam = response.data;
    //     _this.pmsSkuIn.productCategoryId = _this.productParam.productCategoryId;
    //     _this.pmsSkuIn.productCategoryName =
    //       _this.productParam.productCategoryName;
    //     _this.pmsSkuIn.productName = _this.productParam.name;
    //     _this.pmsSkuIn.price = _this.productParam.price;
    //     _this.pmsSkuIn.originalPrice = _this.productParam.originalPrice;
    //     _this.$forceUpdate();
    //   });
    // },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(response => {
              console.log("dddddddd")
              console.log(response);
              console.log(this.isEdit)
              if (this.isEdit) {
                console.log("fffffffff")
                createPmsSkuIn(this.pmsSkuIn).then(response => {
                  console.log(JSON.stringify(response))
                  if (response.code == 200) {
                    this.$refs[formName].resetFields();
                    this.$message({
                      message: "修改成功",
                      type: "success",
                      duration: 1000
                    });
                    this.$router.back();
                  } else {
                    this.$message({
                      message: response.msg,
                      type: "error",
                      duration: 1000
                    });
                  }
                });
              }
            })
            .catch(() => {
              console.log("12121313");
            });
        }
        // if (valid) {
        //   this.$confirm("是否提交数据", "提示", {
        //     confirmButtonText: "确定",
        //     cancelButtonText: "取消",
        //     type: "warning"
        //   })
        //   .then(() => {
        //     if (this.isEdit) {
        //       updatePmsSkuIn(this.$route.query.id, this.pmsSkuIn).then(
        //         response => {
        //           if (response.code == 200) {
        //             this.$refs[formName].resetFields();
        //             this.$message({
        //               message: "修改成功",
        //               type: "success",
        //               duration: 1000
        //             });
        //             this.$router.back();
        //           } else {
        //             this.$message({
        //               message: response.msg,
        //               type: "error",
        //               duration: 1000
        //             });
        //           }
        //         }
        //       );
        //     }
        //      else {
        //       createPmsSkuIn(this.pmsSkuIn).then(response => {
        //         if (response.code == 200) {
        //           this.$refs[formName].resetFields();
        //           this.pmsSkuIn = Object.assign({}, defaultPmsSkuIn);
        //           this.$message({
        //             message: "提交成功",
        //             type: "success",
        //             duration: 1000
        //           });
        //           this.$router.back();
        //         } else {
        //           this.$message({
        //             message: response.msg,
        //             type: "error",
        //             duration: 1000
        //           });
        //         }
        //       });
        //     }
        //   });
        // } else {
        //   this.$message({
        //     message: "验证失败",
        //     type: "error",
        //     duration: 1000
        //   });
        //   return false;
        // }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.pmsSkuIn = Object.assign({}, defaultPmsSkuIn);
    }
  }
};
</script>
<style>
</style>


